import {
  login,
  getOpenid,
  getPrepay,
  payment,
  order,
} from "../../utils/interface";
Page({
  data: {
    paymentList: {},
  },
  onLoad(e) {
    let { amount, orderNumber } = e;
    this.setData({
      paymentList: { amount, orderNumber },
    });

    // this.getOrder()
  },
  // 获取订单信息
  // async getOrder(){
  // let token = wx.getStorageSync('token')
  // let orderData = wx.getStorageSync('newcart')
  // let goodsJsonStr = orderData.map(item =>{
  //   return {
  //     goodsId:item.goodsId,
  //     number:item.count,
  //     propertyChildIds:item.propsId,
  //     logisticsType:0
  //   }
  // })

  // let newStr = JSON.stringify(goodsJsonStr)
  // let {data:res} = await order({token,goodsJsonStr:newStr}).catch(err=>err)
  // console.log(res);
  // this.setData({
  //   paymentList:res.data
  // })
  // },
  // 支付
  async pay() {
    let openid = wx.getStorageSync("openid") || "";
    if (!openid) {
      // 获取code
      let res = await login().catch((err) => err);
      // 获取openid
      let res1 = await getOpenid({ code: res.code }).catch((err) => err);
      openid = res1.data.openid;
      // 保存本地
      wx.setStorageSync("openid", openid);
    }

    let out_trade_no = this.data.paymentList.orderNumber;
    let total_fee = this.data.paymentList.amount;
    // 获取预支付
    let res2 = await getPrepay({
      openid,
      body: "支付测试",
      out_trade_no,
      total_fee,
    }).catch((err) => err);
    let data = res2.data;
    // 调起支付
    let res3 = await payment({ ...data }).catch((err) => err);
  },
});
